<?php

/**
 * This is the model class for table "sd.users".
 *
 * The followings are the available columns in table 'sd.users':
 * @property string $id
 * @property string $login
 * @property string $email
 * @property string $phone
 * @property string $password
 * @property string $name
 * @property string $lastname
 * @property string $date_registration
 * @property string $role
 *
 * The followings are the available model relations:
 * @property Comments[] $comments
 * @property Files[] $files
 * @property News[] $news
 * @property News[] $news1
 * @property Order[] $orders
 * @property Pages[] $pages
 * @property Pages[] $pages1
 * @property Photos[] $photoses
 */
class Users extends CBaseActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sd.users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('login, password, date_registration', 'required'),
			array('login, email, password, lastname', 'length', 'max'=>45),
			array('phone', 'length', 'max'=>15),
			array('name', 'length', 'max'=>100),
			array('role', 'length', 'max'=>7),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, login, email, phone, password, name, lastname, date_registration, role', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'comments' => array(self::HAS_MANY, 'Comments', 'user_id'),
			'files' => array(self::HAS_MANY, 'Files', 'upload_user'),
			'news' => array(self::HAS_MANY, 'News', 'user_add'),
			'news1' => array(self::HAS_MANY, 'News', 'user_edit'),
			'orders' => array(self::HAS_MANY, 'Order', 'user_id'),
			'pages' => array(self::HAS_MANY, 'Pages', 'user_add'),
			'pages1' => array(self::HAS_MANY, 'Pages', 'user_edit'),
			'photoses' => array(self::HAS_MANY, 'Photos', 'upload_user'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'login' => 'Login',
			'email' => 'Email',
			'phone' => 'Phone',
			'password' => 'Password',
			'name' => 'Name',
			'lastname' => 'Lastname',
			'date_registration' => 'Date Registration',
			'role' => 'Role',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('login',$this->login,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('date_registration',$this->date_registration,true);
		$criteria->compare('role',$this->role,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
